package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Integer save(Comment comment) {
        // 1. 插入一个评论的记录
        mongoTemplate.insert(comment);
        // 2. 对点赞的朋友圈的点赞数+1
        Query query = new Query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),1);
        mongoTemplate.updateFirst(query,update,Publish.class);
        // 3. 并且返回最新的（点赞、评论、喜欢）数
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        if(comment.getCommentType()==1){
            //点赞
            return publish.getLikeCount();
        }else if(comment.getCommentType()==2){
            //评论
            return publish.getCommentCount();
        }else{
            return publish.getLoveCount();
        }
    }

    /**
     * 取消点赞(删除一个评论的记录 ,对点赞的朋友圈的点赞数-1,并且返回最新的点赞数)
     * @param comment
     * @return
     */
    @Override
    public Integer delete(Comment comment) {
        //1. 删除一个评论的记录
        Query query = new Query(Criteria.where("publishId").is(comment.getPublishId())
        .and("commentType").is(comment.getCommentType()).and("pubType").is(comment.getPubType())
        .and("userId").is(comment.getUserId()));
        mongoTemplate.remove(query,Comment.class);
        //2 . 对点赞的朋友圈的点赞数-1
        Query publishQuery = new Query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),-1);
        mongoTemplate.updateFirst(publishQuery,update,Publish.class);

        // 3. 并且返回最新的（点赞、评论、喜欢）数
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        if(comment.getCommentType()==1){
            //点赞
            return publish.getLikeCount();
        }else if(comment.getCommentType()==2){
            //评论
            return publish.getCommentCount();
        }else{
            return publish.getLoveCount();
        }
    }

    //根据动态的id查询评论的分页对象
    @Override
    public PageResult queryCommentsList(String publishId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(2));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
       Long  count = mongoTemplate.count(query, Comment.class);
        PageResult pageResult = new PageResult(page,pagesize,count.intValue(),commentList);
        return pageResult;
    }

    @Override
    public PageResult queryByUser(Integer page, Integer pagesize, Long userId, Integer contentType) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Publish> publishes = mongoTemplate.find(query, Publish.class);
        ArrayList<ObjectId> publishId = new ArrayList<>();
        if(publishes != null){
            for (Publish publish : publishes) {
                publishId.add(publish.getId());
            }
        }
        Query q = new Query(Criteria.where("publishId").in(publishId).and("commentType").is(contentType));
        q.with(Sort.by(Sort.Order.desc("created")));
        q.limit(pagesize).skip((page - 1)*pagesize);
        List<Comment> comments = mongoTemplate.find(q, Comment.class);
        long count = mongoTemplate.count(q, Comment.class);
        return new PageResult(page,pagesize,(int)count,comments);
    }


}
